package org.example.erp.controller;

import org.example.erp.domain.Equipment;
import org.example.erp.domain.SalesOrder;
import org.example.erp.service.impl.EquipmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class EquipmentController {
    @Autowired
    private EquipmentServiceImpl equipmentService;
    @GetMapping("/deviceLedger/find")
    public DataResult find(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                        @RequestParam(value = "pageSize",defaultValue = "1") Integer pageSize,
                                        @RequestParam(required = false) String deviceNo,
                                        @RequestParam(required = false) String deviceName){
        return DataResult.success(equipmentService.Find(currentPage, pageSize,deviceNo, deviceName));
    }
    @DeleteMapping("/deviceLedger/delete")
    public DataResult delete(@RequestBody List<Integer> deviceNo) {
        if(deviceNo.isEmpty()){
            equipmentService.remove(null);
        }else{
            equipmentService.removeByIds(deviceNo);
        }
        return DataResult.success();
    }
    @PutMapping("/deviceLedger/update")
    public DataResult updateReversoContexts(@RequestBody Equipment s) {
        equipmentService.updateById(s);
        return DataResult.success();
    }

    @PostMapping("/deviceLedger/insert")
    public DataResult Insert(@RequestBody Equipment s) {
        try {
            equipmentService.save(s);
            return DataResult.success(); // 确保返回成功结果
        } catch (Exception e) {
            return DataResult.success("新增失败：" + e.getMessage()); // 错误处理
        }
    }
}
